package com.ruoyi.system.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.vo.CarInfoVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.system.domain.CarInfo;
import com.ruoyi.system.service.ICarInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆管理Controller
 *
 * @author trip
 * @date 2024-09-10
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/info")
public class CarInfoController extends BaseController {

    private final ICarInfoService carInfoService;

/**
 * 查询车辆管理列表
 */
@PreAuthorize("@ss.hasPermi('system:info:list')")
@GetMapping("/list")
    public TableDataInfo list(CarInfo carInfo) {
        startPage();
    //        LambdaQueryWrapper<CarInfo> lqw = new LambdaQueryWrapper<CarInfo>();
//                        if (StringUtils.isNotBlank(carInfo.getLicensePlate())){
//                        lqw.eq(CarInfo::getLicensePlate ,carInfo.getLicensePlate());
//                    }
//                        if (StringUtils.isNotBlank(carInfo.getColor())){
//                        lqw.eq(CarInfo::getColor ,carInfo.getColor());
//                    }
//                        if (StringUtils.isNotBlank(carInfo.getBrand())){
//                        lqw.eq(CarInfo::getBrand ,carInfo.getBrand());
//                    }
//                        if (carInfo.getSeat() != null){
//                        lqw.eq(CarInfo::getSeat ,carInfo.getSeat());
//                    }
//                        if (StringUtils.isNotBlank(carInfo.getStatus())){
//                        lqw.eq(CarInfo::getStatus ,carInfo.getStatus());
//                    }
//        List<CarInfo> list = carInfoService.list(lqw);
    List<CarInfoVO> list = carInfoService.getCarInfoList(carInfo);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        util.importTemplateExcel(response, "车辆管理数据");
    }

    /**
     * 导入车辆管理数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:info:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        List<CarInfo> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            carInfoService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出车辆管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarInfo carInfo) {
        List<CarInfo> list = carInfoService.selectCarInfoList(carInfo);
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        util.exportExcel(response, list, "车辆管理数据");
    }

    /**
     * 获取车辆管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{carInfoId}")
    public AjaxResult getInfo(@PathVariable("carInfoId") Long carInfoId) {
        return AjaxResult.success(carInfoService.selectCarInfoByCarInfoId(carInfoId));
    }

    /**
     * 新增车辆管理
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarInfo carInfo) {
        return toAjax(carInfoService.insertCarInfo(carInfo));
    }

    /**
     * 修改车辆管理
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarInfo carInfo) {
        return toAjax(carInfoService.updateCarInfo(carInfo));
    }

    /**
     * 删除车辆管理
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carInfoIds}")
    public AjaxResult remove(@PathVariable Long[] carInfoIds) {
        return toAjax(carInfoService.deleteCarInfoByCarInfoIds(carInfoIds));
    }
}


